﻿using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SignalRHubs.Application.Extensions
{
    public static class RedisCacheExtensions
    {
        public static async Task<T> GetObjectAsync<T>(this IDistributedCache cache, string key, CancellationToken token = default)
        {
            var data = await cache.GetStringAsync(key, token);
            if (string.IsNullOrWhiteSpace(data)) return default(T);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(data);
        }

        public static async Task SetObjectAsync(this IDistributedCache cache, string key, object value, CancellationToken token = default)
        {
            await cache.SetStringAsync(key, value.ToJson(), token);
        }
    }
}
